package com.example.demo.jpa.product.dao;

import com.example.demo.jpa.config.exception.CustomException;
import com.example.demo.jpa.config.exception.ExceptionResp;
import com.example.demo.jpa.customer.constants.Sex;
import com.example.demo.jpa.customer.domain.Customer;
import com.example.demo.jpa.customer.dto.CustomerCommand;
import com.example.demo.jpa.customer.dto.CustomerDTO;
import com.example.demo.jpa.customer.repo.CustomerRepository;
import com.example.demo.jpa.org.domain.Org;
import com.example.demo.jpa.org.dto.OrgDto;
import com.example.demo.jpa.product.domain.FinancialProduct;
import com.example.demo.jpa.product.dto.FinancialProductCommand;
import com.example.demo.jpa.product.dto.FinancialProductDTO;
import com.example.demo.jpa.product.repo.FinancialProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.Objects;
import java.util.Optional;
import static java.util.Objects.isNull;

@Component
public class FinancialProductDAO {

    @Autowired
    FinancialProductRepository financialProductRepository;

    public FinancialProductDTO findFinancialProductByID(long id){
        FinancialProduct domain = financialProductRepository.findById(id);
        if (isNull(domain)){
            throw new CustomException(ExceptionResp.FINANCIALPRODUCT_NOT_FOUND,"FinancialProduct not found");
        }
        FinancialProductDTO dto = new FinancialProductDTO(domain.getId(), domain.getName(), domain.getExpectedAnnualReturnRate(),domain.getMinimumInvestmentAmount(),domain.getRiskLevel(),domain.getIssuer());
        return dto;
    }
    @Transactional
    public FinancialProductDTO  createFinancialProduct(FinancialProductCommand financialProductCommand){
        FinancialProduct financialProduct=new FinancialProduct(financialProductCommand.getName(),financialProductCommand.getExpectedAnnualReturnRate(),financialProductCommand.getMinimumInvestmentAmount(),financialProductCommand.getRiskLevel(),financialProductCommand.getIssuer());
        FinancialProduct tmp=financialProductRepository.save(financialProduct);
        FinancialProductDTO dto = new FinancialProductDTO(tmp.getId(), tmp.getName(),tmp.getExpectedAnnualReturnRate(),tmp.getMinimumInvestmentAmount(),tmp.getRiskLevel(),tmp.getIssuer());
        return dto;
    }
    @Transactional
    public FinancialProductDTO updateFinancialProduct(long id,FinancialProductCommand financialProductCommand){

        FinancialProduct c = financialProductRepository.findById(id);
        if(isNull(c)){
            throw new CustomException(ExceptionResp.FINANCIALPRODUCT_NOT_FOUND,"FinancialProduct not found");
        }
        if(financialProductCommand.getName() != null){
            c.setName(financialProductCommand.getName());
        }
        if(financialProductCommand.getExpectedAnnualReturnRate()!=null){
            c.setExpectedAnnualReturnRate(financialProductCommand.getExpectedAnnualReturnRate());
        }
        if(financialProductCommand.getMinimumInvestmentAmount()!=null){
            c.setMinimumInvestmentAmount(financialProductCommand.getMinimumInvestmentAmount());
        }
        if(financialProductCommand.getRiskLevel()!=null){
            c.setRiskLevel(financialProductCommand.getRiskLevel());
        }
        if(financialProductCommand.getIssuer()!=null){
            c.setIssuer(financialProductCommand.getIssuer());
        }
        FinancialProduct t = financialProductRepository.save(c);
        FinancialProductDTO dto = new FinancialProductDTO(t.getId(),t.getName(),t.getExpectedAnnualReturnRate(),t.getMinimumInvestmentAmount(),t.getRiskLevel(),t.getIssuer());
        return dto;
    }
    @Transactional
    public void deleteFinancialProductById(Long id) {
        Optional<FinancialProduct> c = financialProductRepository.findById(id);
        if(c.isEmpty()){
            throw new CustomException(ExceptionResp.FINANCIALPRODUCT_NOT_FOUND,"FinancialProduct not found");
        }else {
            financialProductRepository.deleteFinancialProductById(id);
        }

    }
}
